package com.jiuling.apis.service.basic;

import com.jiuling.apis.model.Hmodel.TBaseMerchant;
import com.jiuling.apis.model.Hmodel.TBaseMerchantStrategy;
import com.jiuling.apis.model.Hmodel.TSysUser;
import com.jiuling.apis.model.Hmodel.basic.TBaseUser;
import com.jiuling.apis.model.Pmodel.Pmerchant;
import com.jswzc.dao.hdao.BaseDaoI;
import com.jswzc.dao.mdao.DaoSupport;
import com.jswzc.framework.util.StringUtil;
import com.jswzc.framework.util.UuidUtil;
import com.jswzc.model.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by tomchen on 17/6/7.
 */
@Service("merchantService")
public class MerchantServiceImpl implements MerchantServiceI {

    @Autowired
    private BaseDaoI<TSysUser> tSysUserDao;

    @Autowired
    private BaseDaoI<TBaseMerchant> tBaseMerchantDao;

    @Autowired
    private BaseDaoI<TBaseMerchantStrategy> tBaseMerchantStrategyDao;

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Override
    public TBaseMerchant getMerchantById(String identity_code) throws Exception {
        return tBaseMerchantDao.getById(TBaseMerchant.class, identity_code);
    }

    /**
     * 查询一级类目
     *
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List<Map<String,Object>> queryCategoryFirst() throws Exception {
        return (List<Map<String,Object>>) dao.findForList("merchantDao.queryCategoryFirst",
                null);
    }

    /**
     * 查询某个类目的子类目
     *
     * @param parent_code
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List<Map<String,Object>> queryCategoryByParent(String parent_code) throws Exception {
        return (List<Map<String,Object>>) dao.findForList("merchantDao.queryCategoryByParent",
                parent_code);
    }

    @Override
    public Map<String, Object> getMerchantWithStrategyById(String identityCode) throws Exception {
        return (Map<String, Object>) dao.findForObject("merchantDao.getMerchantWithStrategyById",identityCode);
    }

    @Override
    public String doSave(Pmerchant pmerchant,String userId,String identityCode) throws Exception {
        String merchantId = "";
        //新增or跟新商户
        if (StringUtil.isNullOrEmpty(pmerchant.getId()) && StringUtil.isNullOrEmpty(identityCode)) {
            TBaseMerchant tBaseMerchant = new TBaseMerchant();
            tBaseMerchant.setLogo(pmerchant.getLogo());
            tBaseMerchant.setName(pmerchant.getName());
            tBaseMerchant.setCategory(pmerchant.getCategory());
            tBaseMerchant.setCategorySub(pmerchant.getCategory_sub());
            tBaseMerchant.setProvince(pmerchant.getProvince());
            tBaseMerchant.setCity(pmerchant.getCity());
            tBaseMerchant.setPhone(pmerchant.getPhone());
            tBaseMerchant.setAddress(pmerchant.getAddress());
            tBaseMerchant.setIsdel(0);
            tBaseMerchant.setState(0);
            tBaseMerchant.setCreateTime(new Date());

            merchantId = (String) tBaseMerchantDao.save(tBaseMerchant);

            //设置tSysUserDao.identityId
            TSysUser tSysUser = tSysUserDao.getById(TSysUser.class, userId);
            tSysUser.setIdentityCode(merchantId);
            tSysUserDao.update(tSysUser);

        }else {
            TBaseMerchant tBaseMerchant = tBaseMerchantDao.getById(TBaseMerchant.class, identityCode);
            tBaseMerchant.setLogo(pmerchant.getLogo());
            tBaseMerchant.setName(pmerchant.getName());
            tBaseMerchant.setCategory(pmerchant.getCategory());
            tBaseMerchant.setCategorySub(pmerchant.getCategory_sub());
            tBaseMerchant.setProvince(pmerchant.getProvince());
            tBaseMerchant.setCity(pmerchant.getCity());
            tBaseMerchant.setPhone(pmerchant.getPhone());
            tBaseMerchant.setAddress(pmerchant.getAddress());

            tBaseMerchantDao.update(tBaseMerchant);
        }


        //新增or跟新商户策略
        if (StringUtil.isNullOrEmpty(pmerchant.getStrategyId()) && StringUtil.isNullOrEmpty(identityCode)) {
            TBaseMerchantStrategy tBaseMerchantStrategy = new TBaseMerchantStrategy();
            tBaseMerchantStrategy.setMerchantId(merchantId);
            tBaseMerchantStrategy.setType(pmerchant.getType());
            if (pmerchant.getType() != 1) {
                tBaseMerchantStrategy.setCouponRate(new BigDecimal(pmerchant.getCouponRate()));
            }
            if (pmerchant.getType() != 0) {
                tBaseMerchantStrategy.setCoinRate(new BigDecimal(pmerchant.getCoinRate()));
            }
            tBaseMerchantStrategy.setIsdel(0);
            tBaseMerchantStrategy.setCreateTime(new Date());

            tBaseMerchantStrategyDao.save(tBaseMerchantStrategy);
        }else{
            TBaseMerchantStrategy tBaseMerchantStrategy = tBaseMerchantStrategyDao.find(StringUtil.formateString("from TBaseMerchantStrategy where merchantId = '{0}'",identityCode)).get(0);
            tBaseMerchantStrategy.setType(pmerchant.getType());
            tBaseMerchantStrategy.setCouponRate(null);
            tBaseMerchantStrategy.setCoinRate(null);
            if (pmerchant.getType() != 1) {
                tBaseMerchantStrategy.setCouponRate(new BigDecimal(pmerchant.getCouponRate()));
            }
            if (pmerchant.getType() != 0) {
                tBaseMerchantStrategy.setCoinRate(new BigDecimal(pmerchant.getCoinRate()));
            }

            tBaseMerchantStrategyDao.update(tBaseMerchantStrategy);
        }

        return merchantId;

    }

    @Override
    public Pager<Map<String, Object>> queryMerchant(Pager<Map<String, Object>> pager) throws Exception {
        return dao.findForPager("merchantDao.queryMerchant","merchantDao.queryMerchantCount",pager);
    }

    @Override
    public void doApprove(Pmerchant pmerchant) throws Exception {
        String merchantId = pmerchant.getId();
        String strategyId = pmerchant.getStrategyId();
        Double feeRate = pmerchant.getFeeRate();

        TBaseMerchant tBaseMerchant = tBaseMerchantDao.getById(TBaseMerchant.class, merchantId);
        tBaseMerchant.setState(1);
        if (StringUtil.isNullOrEmpty(tBaseMerchant.getAuthCode())) {
            tBaseMerchant.setAuthCode(UuidUtil.get32UUID());
        }
        tBaseMerchantDao.update(tBaseMerchant);

        TBaseMerchantStrategy tBaseMerchantStrategy = tBaseMerchantStrategyDao.getById(TBaseMerchantStrategy.class, strategyId);
        tBaseMerchantStrategy.setFeeRate(new BigDecimal(feeRate));
        tBaseMerchantStrategyDao.update(tBaseMerchantStrategy);


    }
}
